﻿<?php
/*
Template Name: Login
*/
get_header();
?>
<div style="color:red;margin-left: 300px;margin-top: 10px;">
<?php
    // Truong hop da nhan button Dang Nhap
    print_r($_POST);
    if($_POST)
    {
        print_r("1");    
        $errorMessage = "";
        $username = $wpdb->escape($_REQUEST['txt_user']);
        $password = $wpdb->escape($_REQUEST['txt_pass']);
        $remember = $wpdb->escape($_REQUEST['rememberme']);
        if(empty($username))
        {
            $errorMessage = "Chua nhap UserName";
        }
        else if(empty($password))
        {
            $errorMessage = "Chua nhap Password";
        }
        else
        {
            // nothing
        }
        print_r($_COOKIE['username']);
        $login_data = array();
        $login_data['user_login'] = $username;
        $login_data['user_password'] = $password;
        $login_data['remember'] = $remember;
        // Truong hop checkbox duoc check se luu lai user va pass
        if($remember)
        {
            // user va pass duoc luu cho lan dang nhap ke tiep trong 1 thang
            //$_COOKIE['username'] = $username;
            //$_COOKIE['password'] = $password;
            //setcookie("username", $username, time() + (30 * 24 * 60 * 60));
            //setcookie("password", $password, time() + (30 * 24 * 60 * 60));
        }
        // Khong check vao checkbox se xoa cookie
        else
        {
            
            if(isset($_COOKIE['username']))
            {
                //setcookie("username", "", time() -3600);
                //setcookie("password", "", time() -3600);
            }
            //$_COOKIE['username'] = "";
            //$_COOKIE['password'] = "";
        }
        $user_verify = wp_signon( $login_data, true );
        print_r($user_verify);
        if (is_wp_error($user_verify) )
        {
            // Truong hop dang nhap sai User or Pass
            if(empty($errorMessage))
            {
                $errorMessage = "Sai ten dang nhap hoac mat khau";
            }
        } 
        else
        {
           //wp_set_current_user($user_verify->ID);
           // Sau khi dang nhap thanh cong thi di chuyen den trang chu
           wp_redirect( home_url() );
?>
           </div>
<?php
           exit();
        }
    }
?>      <!-- Xuat thong bao neu dang nhap xay ra loi... -->
		<div id="login" style="margin-left: 300px;margin-top: 10px;">
        <?php
        if(isset($errorMessage) && !empty($errorMessage))
        {
        ?>
		<div id="result" style="color:red;"><p><?php echo $errorMessage; ?></p></div>
        <?php
        } 
        ?>
    	<form method="post" class="frm_login" name="frm_login" onload="setFocus();">
    		<p><label>Ten Dang Nhap: </label>
    			<input type="text" id="txt_user" class="txt_user" name="txt_user" value="<?php if(isset($login_data)){ echo $login_data["user_login"]; } ?>"/></p>
    		<p><label>Password: </label>
    			<input type="password" class="txt_pass" name="txt_pass" value="<?php if(isset($login_data)){ echo $login_data["user_password"]; } ?>" /></p>
    		<p>
    			<label class="inline">
    				<input type="checkbox" value="forever" class="rememberme" name="rememberme" <?php if(isset($remember) && $remember) { echo "checked='checked'"; } ?>/>
    				Ghi nho	
    			</label><br/>
    			<a class="forgot_pass" href="<?php echo home_url()."/lost-password"; ?>">Quen mat khau?</a>
    		</p>
    		<p>
    			<input type="submit" value="Dang Nhap" class="btn_login" name="btn_login"/>
    		</p>
    	</form>
</div>
</div>
<?php
get_footer();
?>